﻿-- ================================================
-- Template generated from Template Explorer using:
-- Create Trigger (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- See additional Create Trigger templates for more
-- examples of different Trigger statements.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER TRI_MERGEREL
   ON REL_AUTHOR_INSTITUTION
   AFTER INSERT
AS 
DECLARE @AID BIGINT;
DECLARE @IID BIGINT;
DECLARE @CHECK INT;
DECLARE @ID BIGINT;
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- Int32erfering with SELECT statements.
	SET @ID = (SELECT ID FROM INSERTED);
	SET @AID = (SELECT AUTHOR_ID FROM INSERTED);
	SET @IID = (SELECT INSTITUTION_ID FROM INSERTED);
	SET @CHECK = (SELECT COUNT(*) FROM REL_AUTHOR_INSTITUTION WHERE AUTHOR_ID=@AID AND INSTITUTION_ID=@IID);
	IF @CHECK > 1
	BEGIN
		DELETE FROM REL_AUTHOR_INSTITUTION WHERE ID=@ID;
	END
	SET NOCOUNT ON;
    -- Insert statements for trigger here

END
GO
